package txt2bib;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java_cup.runtime.Symbol;
import java_cup.runtime.lr_parser;
import txt2bib.util.Grammars;


/**
 * 
 * @author v
 *
 */
public class CustomParser {
	private lr_parser parser = null;
	private java_cup.runtime.Scanner scanner = null;
	boolean do_debug_parse = false;
	Grammars grammar = null;
	
	public CustomParser(Grammars gr) {
		this.grammar = gr; 
	}
	
	public void createScanner(File file) throws FileNotFoundException, Exception {
		FileReader reader = new FileReader(file);
		switch (grammar) {
		case ACM:
			scanner = new Scanner(reader);
			break;
		case Elsevier:
			scanner = new ScannerElsevier(reader);
			break;
		case Springer:
			scanner = new ScannerSpringer(reader);
			break;
		case IEEE:
			scanner = new ScannerIEEE(reader);
			break;
		case Auto:
			
		default:
			throw new Exception("Scanner is null");
		}
		
	}
	
	public void createParser(java_cup.runtime.Scanner scanner) throws Exception {
		if (scanner != null) {
			switch (grammar) {
			case ACM:
				parser = new GrammarCup(scanner);
				break;
			case Elsevier:
				parser = new GrammarElsevierCup(scanner);
				break;
			case Springer:
				parser = new GrammarSpringerCup(scanner);
				break;
			case IEEE:
				parser = new GrammarIEEECup(scanner);
				break;
			case Auto:
				
			default:
				throw new Exception("Scanner is null");
			}
		}
		else
			throw new Exception("Scanner is null");
	}
	
	public void printScanner() throws Exception {
		Symbol symbol = scanner.next_token();
		while (symbol.sym != 0 )
		{
			System.out.println(symbol + " Riconosciuto : " + symbol.value);
			
			symbol = scanner.next_token();
		}
		
	}
	
	public void parse() throws Exception {		
        if (this.do_debug_parse)
          /*parse_tree = */this.parser.debug_parse();
        else
          /*parse_tree = */this.parser.parse();
	}
	
	
	public lr_parser getParser() {
		return parser;
	}

	public void setParser(lr_parser parser) {
		this.parser = parser;
	}

	public java_cup.runtime.Scanner getScanner() {
		return scanner;
	}

	public void setScanner(java_cup.runtime.Scanner scanner) {
		this.scanner = scanner;
	}

	public boolean isDo_debug_parse() {
		return do_debug_parse;
	}

	public void setDo_debug_parse(boolean do_debug_parse) {
		this.do_debug_parse = do_debug_parse;
	}

	
}
